package com.orange.cetback.controller.admin;

import com.orange.cetback.common.utils.AliOssUtil;
import com.orange.cetback.common.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/admin-api")
@Tag(name = "通用接口")
@Slf4j
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 文件上传接口
     * @param file
     * @return
     */
    @Operation(summary = "上传文件接口")
    @PostMapping("/upload")
    public Result<String> upload(@NonNull MultipartFile file){
        log.info("文件上传：{}", file.getOriginalFilename());
        try {
            // 生成唯一文件名（防止覆盖）
            String originalFilename = file.getOriginalFilename();
            String objectName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
            // 调用OSS工具上传
            String url = aliOssUtil.upload(file.getBytes(), objectName);
            return Result.success(url);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e.getMessage());
        }
        return Result.fail("文件上传失败");
    }
}
